package com.smartlifev30.baiwei.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.smartlifev30.BuildConfig;
import com.smartlifev30.R;
import com.smartlifev30.mvp.module.PrepareActivityModel;
import com.smartlifev30.mvp.presenter.PrepareActivityPresent;
import com.smartlifev30.mvp.view.IPrepareActivityView;
import com.smartlifev30.network.NetChangeObservable;
import com.smartlifev30.utils.SharedPreferencesUtil;

import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.core.ConnectStatus;
import smartlifev30.com.sdk.net.core.IClient;

import static smartlifev30.com.sdk.net.core.ConnectStatus.CONNECT_FAILED;

/**
 * @author tys
 * @date 2018/8/29
 */

public class PrepareActivity extends BaseActivity<PrepareActivityPresent> implements IPrepareActivityView {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prepare);
        initNetwork();
        presenter.selectedActivity(this);
    }


    private void initNetwork() {
        String serverIp = SharedPreferencesUtil.getString(this, SharedPreferencesUtil.SERVER_IP);
        int serverPort = SharedPreferencesUtil.getInt(this, SharedPreferencesUtil.SERVER_PORT);
        NetManager.getInstance().init(this, serverIp, serverPort);
        NetManager.getInstance().connect("114.215.82.238", 17030, new IClient.ConnectListen() {
            @Override
            public void onSuccess() {
                Log.e("0-0- tys", "连接成功");
            }

            @Override
            public void onFailed(Throwable e) {
                Log.e("0-0- tys", "连接失败:" + e.toString());
            }
        });

        NetChangeObservable.register(status -> {
            String result;
            switch (status) {
                case -1:
                    result = "TYPE_NONE";
                    break;
                case 0:
                    result = "TYPE_MOBILE";
                    break;
                case 1:
                    result = "TYPE_WIFI";
                    break;
                default:
                    result = "unknow";
            }

            IClient<String, String> client = NetManager.getInstance().getClient();
            ConnectStatus connectStatus = client.getConnectStatus();
            Log.e("0-0- tys", "当前网络类型:" + result + " 连接状态:" + connectStatus);
            if (connectStatus == ConnectStatus.CONNECT_FAILED && (status == 0 || status == 1)) {
                NetManager.getInstance().connect("114.215.82.238", 17030, new IClient.ConnectListen() {
                    @Override
                    public void onSuccess() {
                        Log.e("0-0- tys", "连接成功");
                    }

                    @Override
                    public void onFailed(Throwable e) {
                        Log.e("0-0- tys", "连接失败:" + e.toString());
                    }
                });
            } else {

            }
        });
    }

    @Override
    protected PrepareActivityPresent createPresent() {
        return new PrepareActivityPresent(this, new PrepareActivityModel());
    }

    @Override
    public void toGuideActivity() {
        Intent intent = new Intent(this, GuideActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void toLoginActivity(@Nullable String arg1, @Nullable Integer arg2) {
        // 1.4g登录超时
        // 2.不是wifi也不是4g
        // 3.token过期,token无效
        // 4.没有记录
        Intent intent = new Intent(this, LoginActivity.class);
        if (!TextUtils.isEmpty(arg1)) {
            intent.putExtra("arg1", arg1);
        }
        if (null != arg2) {
            intent.putExtra("arg2", arg2);
        }
        startActivity(intent);
        finish();
    }

    @Override
    public void toGatewayActivity(@Nullable String arg1, @Nullable Integer arg2) {
        Intent intent = new Intent(this, GatewayActivity.class);
        if (!TextUtils.isEmpty(arg1)) {
            intent.putExtra("arg1", arg1);
        }

        if (null != arg2) {
            intent.putExtra("arg2", arg2);
        }
        startActivity(intent);
        finish();
    }
}
